package utils

import (
	"errors"
	"fmt"
	"github.com/golang-jwt/jwt/v5"

	"time"
)

const EXP = 60 * 60 * 24
const SECRET = "12345"

func GetJwtToken(payload int64) (string, error) {

	iat := time.Now().Unix()
	claims := make(jwt.MapClaims)
	claims["exp"] = iat + EXP
	claims["iat"] = iat
	claims["payload"] = payload
	token := jwt.New(jwt.SigningMethodHS256)
	token.Claims = claims
	return token.SignedString([]byte(SECRET))

}
func ParseJwtToken(tokenString string) (int64, error) {

	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (any, error) {
		return []byte(SECRET), nil
	})

	switch {
	case token.Valid:
		fmt.Println("You look nice today")
	case errors.Is(err, jwt.ErrTokenMalformed):
		fmt.Println("That's not even a token")
	case errors.Is(err, jwt.ErrTokenSignatureInvalid):
		// Invalid signature
		fmt.Println("Invalid signature")
	case errors.Is(err, jwt.ErrTokenExpired) || errors.Is(err, jwt.ErrTokenNotValidYet):
		// Token is either expired or not active yet
		fmt.Println("Timing is everything")
	default:
		fmt.Println("Couldn't handle this token:", err)
	}

	//if claims, ok := token.Claims.(jwt.MapClaims); ok {
	//	playload := claims["payload"]
	//} else {
	fmt.Println(err)
	//	}
	return 0, err
}
